package com.example.vo;

import com.example.util.StringUtil;
import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
public abstract class BaseVo implements Serializable {

    private static final long serialVersionUID = -2237497567339625860L;

    private static final String ASC = "asc";
    private static final String DESC = "desc";
    /**
     * 当前页码
     */
    private int pi = 0;
    /**
     * 	每页数量，当设置为 0 表示不分页，默认：10
     */
    private int ps = 10;
    /**
     * 排序字段
     */
    private String sort;
    /**
     * 排序规则
     */
    private String order;

    private String id;

    private Date createDate;

    private Date modifyDate;

    public abstract List<QueryParam> buildQueryParams();

    public PageRequest buildPageRequest() {
        Sort jpaSort = null;
        if (StringUtil.isNotBlank(sort) && StringUtil.isNotBlank(order)) {
            if (DESC.equals(order)) {
                jpaSort = new Sort(Sort.Direction.DESC, sort);
            } else if (ASC.equals(order)) {
                jpaSort = new Sort(Sort.Direction.ASC, sort);
            }
        } else {
            jpaSort = new Sort(Sort.Direction.ASC, "createDate");
        }
        return PageRequest.of(pi / ps, ps, jpaSort);
    }
}
